/**
 * DateFormatter
 *
 * @author Chris Pratt
 *
 * 11/26/2009
 */
package com.anodyzed.onyx.text;

import com.anodyzed.onyx.util.Convert;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateFormatter extends TextFormatter {
  protected DateFormat fmt;

  /**
   * Constructor
   *  
   * @param parent The parent TextFormat 
   * @param key The Argument Index or Map Key for this node 
   * @param bean The additional Bean reference String 
   */
  public DateFormatter (TextFormat parent,CharSequence key,CharSequence bean) {
    super(parent,key,bean);
    fmt = DateFormat.getInstance();
  } //DateFormatter

  /**
   * Constructor
   *  
   * @param parent The parent TextFormat 
   * @param key The Argument Index or Map Key for this node 
   * @param bean The additional Bean reference String 
   * @param style The Date Style
   */
  public DateFormatter (TextFormat parent,CharSequence key,CharSequence bean,CharSequence style) {
    super(parent,key,bean);
    if(style.equals("short")) {
      fmt = DateFormat.getDateInstance(DateFormat.SHORT);
    } else if(style.equals("medium")) {
      fmt = DateFormat.getDateInstance(DateFormat.MEDIUM);
    } else if(style.equals("long")) {
      fmt = DateFormat.getDateInstance(DateFormat.LONG);
    } else if(style.equals("full")) {
      fmt = DateFormat.getDateInstance(DateFormat.FULL);
    } else {
      fmt = new SimpleDateFormat(style.toString());
    }
  } //DateFormatter

  /**
   * Return a string representation of a date object
   *
   * @param args The Formatting Arguments
   * @return The Formatted String
   */
  @Override
  public String toString (Object... args) {
    Object o = getValue(args);
    if(o instanceof Date) {
      return fmt.format((Date)o);
    } else if(o instanceof Calendar) {
      return fmt.format(((Calendar)o).getTime());
    } else if(o instanceof String) {
      return fmt.format(Convert.toDate((String)o));
    } else if(o instanceof Number) {
      return fmt.format(new Date(((Number)o).longValue()));
    }
    return null;
  } //toString

} //*DateFormatter
